package com.tender;

import com.tender.utils.ESOperationTemplate;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;

public class A_8_BatchInsertDoc {

    public static void main(String[] args) throws Exception {
        ESOperationTemplate.process(client -> {

            try {
                // 创建批量新增请求对象
                BulkRequest request = new BulkRequest();
                request.add(new IndexRequest().index("user2").id("1001").source(XContentType.JSON, "name", "zhangsan", "age", "10", "sex","女"));
                request.add(new IndexRequest().index("user2").id("1002").source(XContentType.JSON, "name", "lisi", "age", "30", "sex","女"));
                request.add(new IndexRequest().index("user2").id("1003").source(XContentType.JSON, "name", "wangwu1", "age", "40", "sex","男"));
                request.add(new IndexRequest().index("user2").id("1004").source(XContentType.JSON, "name", "wangwu2", "age", "20", "sex","女"));
                request.add(new IndexRequest().index("user2").id("1005").source(XContentType.JSON, "name", "wangwu3", "age", "50", "sex","男"));
                request.add(new IndexRequest().index("user2").id("1006").source(XContentType.JSON, "name", "wangwu4", "age", "20", "sex","男"));
                //客户端发送请求，获取响应对象
                BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
                // 打印结果信息
                System.out.println("took:" + responses.getTook());
                System.out.println("items:" + responses.getItems());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return null;
        });
    }
}
